<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="家具名称" prop="furnitureName">
        <el-input
          v-model="queryParams.furnitureName"
          placeholder="请输入家具名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家具类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择家具类型" clearable>
          <el-option
            v-for="dict in dict.type.furniture_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="家具风格" prop="styleId">
        <el-select v-model="queryParams.styleId" placeholder="请选择家具风格" clearable>
          <el-option
            v-for="dict in dict.type.furniture_style"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="家具品牌" prop="brandId">
        <el-select v-model="queryParams.brandId" placeholder="请选择家具品牌" clearable>
          <el-option
            v-for="dict in dict.type.furniture_brand"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="家具材料" prop="material">
        <el-input
          v-model="queryParams.material"
          placeholder="请输入家具材料"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家具的规格尺寸" prop="size">
        <el-input
          v-model="queryParams.size"
          placeholder="请输入家具的规格尺寸"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家具的租赁价格" prop="rentalPrice">
        <el-date-picker clearable
          v-model="queryParams.rentalPrice"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择家具的租赁价格">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="家具所需的押金金额" prop="depositRequirement">
        <el-date-picker clearable
          v-model="queryParams.depositRequirement"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择家具所需的押金金额">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="配送家具的费用" prop="deliveryFeeRule">
        <el-input
          v-model="queryParams.deliveryFeeRule"
          placeholder="请输入配送家具的费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否租赁" prop="isAvailable">
        <el-input
          v-model="queryParams.isAvailable"
          placeholder="请输入是否租赁"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:furniture:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:furniture:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:furniture:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:furniture:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="furnitureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="家具唯一标识ID" align="center" prop="furnitureId" />
      <el-table-column label="家具名称" align="center" prop="furnitureName" />
      <el-table-column label="家具类型" align="center" prop="typeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.furniture_type" :value="scope.row.typeId"/>
        </template>
      </el-table-column>
      <el-table-column label="家具风格" align="center" prop="styleId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.furniture_style" :value="scope.row.styleId"/>
        </template>
      </el-table-column>
      <el-table-column label="家具品牌" align="center" prop="brandId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.furniture_brand" :value="scope.row.brandId"/>
        </template>
      </el-table-column>
      <el-table-column label="家具材料" align="center" prop="material" />
      <el-table-column label="家具的颜色ID" align="center" prop="colorId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.furniture_color" :value="scope.row.colorId ? scope.row.colorId.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="家具的规格尺寸" align="center" prop="size" />
      <el-table-column label="家具图片" align="center" prop="imagePaths" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imagePaths" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="家具的租赁价格" align="center" prop="rentalPrice" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentalPrice, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家具所需的押金金额" align="center" prop="depositRequirement" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.depositRequirement, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="配送家具的费用" align="center" prop="deliveryFeeRule" />
      <el-table-column label="家具库存数量" align="center" prop="stock" />
      <el-table-column label="是否租赁" align="center" prop="isAvailable" />
      <el-table-column label="家具详细信息" align="center" prop="detailedDescription" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:furniture:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:furniture:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家具管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="家具名称" prop="furnitureName">
          <el-input v-model="form.furnitureName" placeholder="请输入家具名称" />
        </el-form-item>
        <el-form-item label="家具类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择家具类型">
            <el-option
              v-for="dict in dict.type.furniture_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家具风格" prop="styleId">
          <el-select v-model="form.styleId" placeholder="请选择家具风格">
            <el-option
              v-for="dict in dict.type.furniture_style"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家具品牌" prop="brandId">
          <el-select v-model="form.brandId" placeholder="请选择家具品牌">
            <el-option
              v-for="dict in dict.type.furniture_brand"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家具材料" prop="material">
          <el-input v-model="form.material" placeholder="请输入家具材料" />
        </el-form-item>
        <el-form-item label="家具的颜色ID" prop="colorId">
          <el-checkbox-group v-model="form.colorId">
            <el-checkbox
              v-for="dict in dict.type.furniture_color"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="家具的规格尺寸" prop="size">
          <el-input v-model="form.size" placeholder="请输入家具的规格尺寸" />
        </el-form-item>
        <el-form-item label="家具图片" prop="imagePaths">
          <image-upload v-model="form.imagePaths"/>
        </el-form-item>
        <el-form-item label="家具的租赁价格" prop="rentalPrice">
          <el-date-picker clearable
            v-model="form.rentalPrice"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择家具的租赁价格">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="家具所需的押金金额" prop="depositRequirement">
          <el-date-picker clearable
            v-model="form.depositRequirement"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择家具所需的押金金额">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="配送家具的费用" prop="deliveryFeeRule">
          <el-input v-model="form.deliveryFeeRule" placeholder="请输入配送家具的费用" />
        </el-form-item>
        <el-form-item label="家具库存数量" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入家具库存数量" />
        </el-form-item>
        <el-form-item label="是否租赁" prop="isAvailable">
          <el-input v-model="form.isAvailable" placeholder="请输入是否租赁" />
        </el-form-item>
        <el-form-item label="家具详细信息">
          <editor v-model="form.detailedDescription" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFurniture, getFurniture, delFurniture, addFurniture, updateFurniture } from "@/api/system/furniture";

export default {
  name: "Furniture",
  dicts: ['furniture_brand', 'furniture_style', 'furniture_type', 'furniture_color'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家具管理表格数据
      furnitureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        furnitureName: null,
        typeId: null,
        styleId: null,
        brandId: null,
        material: null,
        colorId: null,
        size: null,
        rentalPrice: null,
        depositRequirement: null,
        deliveryFeeRule: null,
        isAvailable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        furnitureName: [
          { required: true, message: "家具名称不能为空", trigger: "blur" }
        ],
        typeId: [
          { required: true, message: "家具类型不能为空", trigger: "change" }
        ],
        rentalPrice: [
          { required: true, message: "家具的租赁价格不能为空", trigger: "blur" }
        ],
        depositRequirement: [
          { required: true, message: "家具所需的押金金额不能为空", trigger: "blur" }
        ],
        stock: [
          { required: true, message: "家具库存数量不能为空", trigger: "blur" }
        ],
        isAvailable: [
          { required: true, message: "是否租赁不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询家具管理列表 */
    getList() {
      this.loading = true;
      listFurniture(this.queryParams).then(response => {
        this.furnitureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        furnitureId: null,
        furnitureName: null,
        typeId: null,
        styleId: null,
        brandId: null,
        material: null,
        colorId: [],
        size: null,
        imagePaths: null,
        rentalPrice: null,
        depositRequirement: null,
        deliveryFeeRule: null,
        stock: null,
        isAvailable: null,
        detailedDescription: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.furnitureId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家具管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const furnitureId = row.furnitureId || this.ids
      getFurniture(furnitureId).then(response => {
        this.form = response.data;
        this.form.colorId = this.form.colorId.split(",");
        this.open = true;
        this.title = "修改家具管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.colorId = this.form.colorId.join(",");
          if (this.form.furnitureId != null) {
            updateFurniture(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFurniture(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const furnitureIds = row.furnitureId || this.ids;
      this.$modal.confirm('是否确认删除家具管理编号为"' + furnitureIds + '"的数据项？').then(function() {
        return delFurniture(furnitureIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/furniture/export', {
        ...this.queryParams
      }, `furniture_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
